﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Demo
{
    public class TestQueue
    {

        public static void add()
        {
            RedisInAction.Queue queueIn = new RedisInAction.Queue("In");
            RedisInAction.Queue queueOut = new RedisInAction.Queue("Out");
            queueIn.Clear();
            queueOut.Clear();
            for (int i = 0; i < 1500; i++)
            {
                queueIn.Enqueue(i.ToString());
            }
            Console.WriteLine($"队列queueIn中元素个数{queueIn.Count} 队列queueIn中元素个数{queueOut.Count}");
            List<Task> list = new List<Task>();
            Action action = () =>
            {
                while (queueIn.Count > 0)
                {
                    string data = null;
                    try
                    {
                        data = queueIn.Dequeue();
                        if (data == null)
                        {
                            break;
                        }
                        Task.Delay(200);
                        if (Y.Common.RandomString.CreateIDLong() % 7 == 0)
                        {
                            throw new Exception("");
                        }
                        queueOut.Enqueue(data);
                    }
                    catch
                    {
                        if (data != null)
                        {
                            queueIn.Enqueue(data);
                        }
                    }
                }
            };
            for (int i = 0; i < 10; i++)
            {
                list.Add(Task.Run(action));
            }
            Task.WaitAll(list.ToArray());
            Console.WriteLine($"队列queueIn中元素个数{queueIn.Count} 队列queueIn中元素个数{queueOut.Count}");
            // queue.Clear();
        }


    }
}
